package com.cy.lib.manager;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author CY
 * @date 2014-11-24
 * @package com.src.zf.doudou.manager
 */
public class NetWorkManager extends Manager {

    Application mApp;
    BroadcastReceiver mReceiver;
    Set<OnNetworkListener> mSetListener;

    public NetType mNetType;

    public enum NetType {

        wifi, mobile
    }

    public NetState mNetState;

    public enum NetState {
        noConnect, connected
    }

    protected NetWorkManager(Application context) {
        mApp = context;
//        regiseter();
    }

    public static NetWorkManager getInstance() {
        return (NetWorkManager) Manager.getInstance().getLocalService(Manager.LocalService.NETWORK);
    }

    public static boolean isConnected(Context context) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
        return info != null && info.isAvailable() && info.isConnected();
    }

    public boolean isConnected() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) mApp.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
        return info != null && info.isAvailable() && info.isConnected();
    }

    private void regiseter() {
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (noConnectivity) {
                    mNetState = NetState.noConnect;
                    if (mSetListener != null) {
                        for (OnNetworkListener listener : mSetListener) {
                            listener.disconnect();
                        }
                    }
                } else {
                    ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
                    if (info == null) {
                        return;
                    }
                    mNetState = NetState.connected;
                    switch (info.getType()) {
                        case ConnectivityManager.TYPE_MOBILE:
                            mNetType = NetType.mobile;
                            break;
                        case ConnectivityManager.TYPE_WIFI:
                            mNetType = NetType.wifi;
                            break;
                    }
                    if (mSetListener != null) {
                        for (OnNetworkListener listener : mSetListener) {
                            listener.connected(info);
                        }
                    }
                }
            }
        };
        mApp.registerReceiver(mReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    public void registerNetworkListener(OnNetworkListener listener) {
        if (mSetListener == null) {
            mSetListener = new LinkedHashSet<OnNetworkListener>();
        }
        synchronized (mSetListener) {
            if (!mSetListener.contains(listener)) {
                mSetListener.add(listener);
            }
        }
    }

    public void unRegisterNetworkListener(OnNetworkListener listener) {
        synchronized (mSetListener) {
            if (mSetListener != null && mSetListener.contains(listener)) {
                mSetListener.remove(listener);
            }
        }
    }

    public interface OnNetworkListener {

        public void disconnect();

        public void connected(NetworkInfo info);
    }
}
